﻿using Cl.Model.DataBaseEntityDtos.Tests.TemplateDemos;
using Cl.Model.Results;
using Cl.Utils.Helper;
using Cl.Utils;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cl.Entity.DataBaseEntity.Test;
using Cl.Repository;
using Cl.Utils.Helper.AutoMap;

namespace Cl.Service.Service.Test.TemplateDemos
{
    public class TemplateDemoService : ITemplateDemoService
    {
        private readonly ClRepository<TemplateDemo> _TemplateDemoRepository;

        public TemplateDemoService(ClRepository<TemplateDemo> TemplateDemoRepository)
        {
            _TemplateDemoRepository = TemplateDemoRepository;
        }

        public async Task<ResultInfoModel> Delete(List<int> ids)
        {
            try
            {
                //数据准备
                var result = await _TemplateDemoRepository.AsUpdateable(new TemplateDemo() { DeletedId = GlobalUserManager.UserId, DeleteTime = DateTime.Now })
                    .UpdateColumns(it => new { it.DeleteTime, it.DeletedId })
                    .Where(x => ids.Contains(x.Id)).ExecuteCommandAsync();
                return GetResult.Success();
            }
            catch (Exception ex)
            {
                LoggerHelper.TryCatch(ex);
                return GetResult.Error(ex.Message);
            }
        }

        public async Task<PageResultModel<TemplateDemoOutputDto>> Query(SearchTemplateDemoDto request)
        {
            RefAsync<int> total = 0;
            var response = new PageResultModel<TemplateDemoOutputDto>
            {
                Result = new List<TemplateDemoOutputDto>()
            };

            var sql = _TemplateDemoRepository.AsQueryable()
                .Includes(c => c.CreateUser);

            //sql.WhereIF(request.Target != null, x => x.Target == request.Target);

            sql.OrderBy(p => p.CreateTime, OrderByType.Desc);
            // 计算开始和结束索引
            List<TemplateDemo> list;
            if (request.IsPage)
            {
                list = await sql.ToPageListAsync(request.PageIndex, request.PageSize, total);
            }
            else
            {
                list = await sql.ToListAsync();
                total = list.Count;
            }

            response.TotalCount = total;
            response.Result = list.Select(x => new TemplateDemoOutputDto(x) { }).ToList();
            return response;
        }

        public async Task<ResultInfoModel> Save(TemplateDemoInputDto obj)
        {
            try
            {
                if (obj == null)
                    return GetResult.Error("参数不能为空");

                //数据准备
                var operationData = obj.MapTo<TemplateDemoInputDto, TemplateDemo>();
                operationData.CreateId = GlobalUserManager.UserId;
                operationData.CreateTime = DateTime.Now;
                var result = await _TemplateDemoRepository.AsInsertable(operationData).ExecuteCommandAsync();
                return GetResult.Success();
            }
            catch (Exception ex)
            {
                LoggerHelper.TryCatch(ex);
                return GetResult.Error(ex.Message);
            }
        }

        public async Task<ResultInfoModel> Update(TemplateDemoInputDto obj)
        {
            try
            {
                if (obj == null)
                    return GetResult.Error("参数不能为空");

                //数据准备
                var operationData = obj.MapTo<TemplateDemoInputDto, TemplateDemo>();
                operationData.UpdateId = GlobalUserManager.UserId;
                operationData.UpdateTime = DateTime.Now;
                var result = await _TemplateDemoRepository.AsUpdateable(operationData).IgnoreColumns(it => new { it.CreateId, it.CreateTime }).ExecuteCommandAsync();
                return GetResult.Success();
            }
            catch (Exception ex)
            {
                LoggerHelper.TryCatch(ex);
                return GetResult.Error(ex.Message);
            }
        }
    }
}
